package org.bingo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.bingo.dto.AddCommentDto;
import org.bingo.dto.CommentPageable;
import org.bingo.dto.CommentUserDto;
import org.bingo.entity.Comment;
import org.bingo.misc.Result;
import org.bingo.misc.ResultState;
import org.bingo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/top/{videoId}")
    public Result getLevel1Comment(@PathVariable("videoId") Long videoId,
                                   @RequestBody CommentPageable commentPageable) {
        List<Comment> level1Comment = commentService.getLevel1Comment(videoId,
                commentPageable.getPage(),
                commentPageable.getCountPerPage());

        List<CommentUserDto> commentUserDtoList = new ArrayList<>();
        level1Comment.forEach(comment -> {
            CommentUserDto commentUserDto = new CommentUserDto();
            commentUserDto.setId(comment.getId());
            commentUserDto.setComment(comment.getComment());
            commentUserDto.setCommentLevel(comment.getCommentLevel());
            commentUserDto.setUserId(comment.getUser().getId());
            commentUserDto.setAvatarUrl(comment.getUser().getAvatarUrl());
            commentUserDto.setUsername(comment.getUser().getUsername());
            commentUserDtoList.add(commentUserDto);
        });
        return Result.build(ResultState.OK, commentUserDtoList);
    }

//    @GetMapping("/level2/{videoId}")
//    public Result getLevel2Comment(@PathVariable("videoId") Long commentId) {
//        List<Comment> level1Comment = commentService.getLevel2Comment(commentId);
//
//        List<CommentUserDto> commentUserDtoList = new ArrayList<>();
//        level1Comment.forEach(comment -> {
//            CommentUserDto commentUserDto = new CommentUserDto();
//            commentUserDto.setId(comment.getId());
//            commentUserDto.setComment(comment.getComment());
//            commentUserDto.setCommentLevel(comment.getCommentLevel());
//            commentUserDto.setUserId(comment.getUser().getId());
//            commentUserDto.setAvatarUrl(comment.getUser().getAvatarUrl());
//            commentUserDto.setUsername(comment.getUser().getUsername());
//            commentUserDtoList.add(commentUserDto);
//        });
//        return Result.build(ResultState.OK, commentUserDtoList);
//    }

    @SaCheckLogin
    @PostMapping("/level1/{videoId}")
    public Result addLevel1Comment(@PathVariable("videoId") Long videoId,
                                   @RequestBody AddCommentDto addCommentDto) {
        Long commentId = commentService.addLevel1Comment(videoId, addCommentDto.getComment());
        if (commentId == -1L) {
            return Result.build(ResultState.INVALID_VIDEO_ID, null);
        } else if (commentId == -2L) {
            return Result.build(ResultState.INVALID_USER_ID, null);
        }

        Comment comment = commentService.getCommentById(commentId);
        CommentUserDto commentUserDto = new CommentUserDto();
        commentUserDto.setCommentLevel(comment.getCommentLevel());
        commentUserDto.setComment(comment.getComment());
        commentUserDto.setId(commentId);
        commentUserDto.setUserId(comment.getUser().getId());
        commentUserDto.setUsername(comment.getUser().getUsername());
        commentUserDto.setAvatarUrl(comment.getUser().getAvatarUrl());
        return Result.build(ResultState.OK, commentUserDto);
    }

//    @SaCheckLogin
//    @PostMapping("/level2/{commentId}")
//    public Result addLevel2Comment(@PathVariable("commentId") Long commentId,
//                                   @RequestBody AddCommentDto addCommentDto) {
//        Long level2Comment = commentService.addLevel2Comment(commentId, addCommentDto.getComment());
//        if (commentId == -1L) {
//            return Result.build(ResultState.INVALID_VIDEO_ID, null);
//        } else if (commentId == -2L) {
//            return Result.build(ResultState.INVALID_USER_ID, null);
//        }
//
//        Comment comment = commentService.getCommentById(level2Comment);
//        CommentUserDto commentUserDto = new CommentUserDto();
//        commentUserDto.setCommentLevel(comment.getCommentLevel());
//        commentUserDto.setComment(comment.getComment());
//        commentUserDto.setId(level2Comment);
//        commentUserDto.setUserId(comment.getUser().getId());
//        commentUserDto.setUsername(comment.getUser().getUsername());
//        commentUserDto.setAvatarUrl(comment.getUser().getAvatarUrl());
//        return Result.build(ResultState.OK, commentUserDto);
//    }

    @SaCheckLogin
    @DeleteMapping("/delete/{commentId}")
    public Result deleteComment(@PathVariable("commentId") Long commentId) {
        Long state = commentService.delComment(commentId);
        if (state == -1) {
            return Result.build(ResultState.INVALID_COMMENT_ID, null);
        } else if (state == -2) {
            return Result.build(ResultState.COMMENT_USER_ID_NOTEQ_LOGIN_USER_ID, null);
        }

        return Result.build(ResultState.OK, 0);
    }
}
